package com.yjt.fresh.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.yjt.fresh.bean.BeanUserInfo;

import java.util.List;
import java.util.Set;

/**
 * 描述：SharedPreferences，存储本地信息
 * 作者：gxcheng
 * 日期： 2017/3/2 0002.15:17
 */

public class PreferenceManager {
    /**
     * 保存Preference的name
     */
    private static final String PREFERENCE_NAME = "userInfo";
    private static final String LONGITUDE = "longitude";
    private static final String LATITUDE = "latitude";
    private static final String IS_LOGIN = "isLogin";
    private static final String USER_INFO = "userInfo";
    private static final String REST_MONEY = "restMoney";
    private static SharedPreferences mSharedPreferences;
    private static PreferenceManager mPreferencemManager;
    private static SharedPreferences.Editor mEditor;

    /*是否登录*/
    private static String SA_CURRENT_USER_IS_LOGIN = "SA_CURRENT_USER_IS_LOGIN";
    /*当前登陆账号*/
    private static String SA_CURRENT_USER_ACCOUNT="SA_CURRENT_USER_ACCOUNT";
    /*当前登陆用户名*/
    private static String SA_CURRENT_USER_NAME="SA_CURRENT_USER_NAME";
    /*设置记录人*/
    private static String SA_CURRENT_RECORDER="SA_CURRENT_RECORDER";
    private static String SA_CURRENT_UNIT_ID="SA_CURRENT_UNIT_ID";
    private static String SA_CURRENT_UNIT_NAME="SA_CURRENT_UNIT_NAME";
    private static String SA_CURRENT_ID="SA_CURRENT_ID";
    private static String SA_ROLE_CODE="SA_ROLE_CODE";
    private static String WXPAY_RESULT = "WXPayResult";
    private static String IS_FIRST_INSTALL="isFirstInstall";//是否第一次安装
    //用户是否第一次启动
    private static String SA_CURRENT_USER_IS_FIRST_LAUNCHER="SA_CURRENT_USER_IS_FIRST_LAUNCHER";
    //网络状态
    private static String SA_IS_NET="SA_IS_NET";
    //存储上一次登陆
    private static final String PRODUCT_SEARCH_HISTORY = "productSearchHistory";
    private static final String STORE_SEARCH_HISTORY = "storeSearchHistory";
    private static final String CITY = "city";


    private PreferenceManager(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public static synchronized void init(Context cxt) {
        if (mPreferencemManager == null) {
            mPreferencemManager = new PreferenceManager(cxt);
        }
    }

    /**
     * 单例模式，获取instance实例
     *
     * @param
     * @return
     */
    public synchronized static PreferenceManager getInstance() {
        if (mPreferencemManager == null) {
            throw new RuntimeException("please init first!");
        }

        return mPreferencemManager;
    }

    public boolean getIsLogin() {
        return mSharedPreferences.getBoolean(IS_LOGIN,false);
    }

    public void setIsLogin(boolean isLogin) {
        mEditor.putBoolean(IS_LOGIN,isLogin);
        mEditor.apply();
    }
    public Boolean getSaCurrentUserIsLogin() {
        return mSharedPreferences.getBoolean(SA_CURRENT_USER_IS_LOGIN,false);
    }

    public void setSaCurrentUserIsLogin(boolean saCurrentUserIsLogin) {
        mEditor.putBoolean(SA_CURRENT_USER_IS_LOGIN,saCurrentUserIsLogin);
        mEditor.apply();
    }

    public String getSaCurrentUserName() {
        return mSharedPreferences.getString(SA_CURRENT_USER_NAME,null);
    }

    public boolean getIsFirstInstall() {
        return mSharedPreferences.getBoolean(IS_FIRST_INSTALL,true);
    }

    public void setIsFirstInstall(boolean isFirstInstall) {
        mEditor.putBoolean(IS_FIRST_INSTALL,isFirstInstall);
        mEditor.apply();
    }

    public void setRestMoney(double restMoney){
        mEditor.putString(REST_MONEY,restMoney+"");
        mEditor.apply();
    }
    public double getRestMoney(){
        return Double.valueOf(mSharedPreferences.getString(REST_MONEY,"0"));
    }
    public void setSaCurrentUserName(String saCurrentUserName) {
        mEditor.putString(SA_CURRENT_USER_NAME,saCurrentUserName);
        mEditor.apply();
    }

    public String getSaCurrentRecorder() {
        return mSharedPreferences.getString(SA_CURRENT_RECORDER,null);
    }

    public BeanUserInfo getUserInfo(){
        String userInfo= mSharedPreferences.getString(USER_INFO,"");
        if (TextUtils.isEmpty(userInfo)) {
            return null;
        }
        return new Gson().fromJson(userInfo, BeanUserInfo.class);
    }

    public void setUserInfo(BeanUserInfo beanUser){
        String userStr=new Gson().toJson(beanUser);
        mEditor.putString(USER_INFO,userStr);
        mEditor.apply();
    }
    public void setSaCurrentRecorder(String saCurrentRecorder) {
        mEditor.putString(SA_CURRENT_RECORDER,saCurrentRecorder);
        mEditor.apply();
    }

    public String getSaCurrentUserAccount() {
        return mSharedPreferences.getString(SA_CURRENT_USER_ACCOUNT,null);
    }

    public void setSaCurrentUserAccount(String saCurrentUserAccount) {
        mEditor.putString(SA_CURRENT_USER_ACCOUNT,saCurrentUserAccount);
        mEditor.apply();
    }

    public String getSaCurrentId() {
        return mSharedPreferences.getString(SA_CURRENT_ID,null);
    }

    public void setSaCurrentId(String saCurrentUserId) {
        mEditor.putString(SA_CURRENT_ID,saCurrentUserId);
        mEditor.apply();
    }

    public String getSaCurrentUnitId() {
        return mSharedPreferences.getString(SA_CURRENT_UNIT_ID,null);
    }

    public void setSaCurrentUnitId(String saCurrentUnitId) {
        mEditor.putString(SA_CURRENT_UNIT_ID,saCurrentUnitId);
        mEditor.apply();
    }

    public String getSaCurrentUnitName() {
        return mSharedPreferences.getString(SA_CURRENT_UNIT_NAME,null);
    }

    public void setSaCurrentUnitName(String saCurrentUnitName) {
        mEditor.putString(SA_CURRENT_UNIT_NAME,saCurrentUnitName);
        mEditor.apply();
    }

    public String getSaRoleCode() {
        return mSharedPreferences.getString(SA_ROLE_CODE,null);
    }

    public void setSaRoleCode(String saRoleCode) {
        mEditor.putString(SA_ROLE_CODE,saRoleCode);
        mEditor.apply();
    }

    public boolean isSaIsNet() {
        return mSharedPreferences.getBoolean(SA_IS_NET,false);
    }

    public void setSaIsNet(boolean saIsNet) {
        mEditor.putBoolean(SA_IS_NET,saIsNet);
        mEditor.apply();
    }

    public boolean getSaCurrentUserIsFirstLauncher() {
        return mSharedPreferences.getBoolean(SA_CURRENT_USER_IS_FIRST_LAUNCHER,true);
    }

    public void setSaCurrentUserIsFirstLauncher(Boolean saCurrentUserIsFirstLauncher) {
        mEditor.putBoolean(SA_CURRENT_USER_IS_FIRST_LAUNCHER,saCurrentUserIsFirstLauncher);
        mEditor.apply();
    }

    public void setProductSearchHistory(String productSearchHistory){
        mEditor.putString(PRODUCT_SEARCH_HISTORY,productSearchHistory);
        mEditor.apply();
    }

    public String getProductSearchHistory(){
        return mSharedPreferences.getString(PRODUCT_SEARCH_HISTORY,"");
    }

    public void clearProductSearchHistory(){
        mEditor.remove(PRODUCT_SEARCH_HISTORY);
        mEditor.commit();
    }

    public void setStoreSearchHistory(String productSearchHistory){
        mEditor.putString(STORE_SEARCH_HISTORY,productSearchHistory);
        mEditor.apply();
    }

    public String getStoreSearchHistory(){
        return mSharedPreferences.getString(STORE_SEARCH_HISTORY,"");
    }

    public void clearStoreSearchHistory(){
        mEditor.remove(STORE_SEARCH_HISTORY);
        mEditor.commit();
    }
    public void clearUserInfo(){
        mEditor.remove(USER_INFO);
        mEditor.commit();
    }

    public void setCity(String city){
        mEditor.putString(CITY,city);
        mEditor.apply();
    }
    public String getCity() {
        return mSharedPreferences.getString(CITY,"");
    }

    public void setLongitude(String longitude){
        mEditor.putString(LONGITUDE,longitude);
        mEditor.apply();
    }

    public String getLongitude(){
        return mSharedPreferences.getString(LONGITUDE,"");
    }

    public void setLatitude(String latitude){
        mEditor.putString(LATITUDE,latitude);
        mEditor.apply();
    }

    public String getLatitude(){

        return mSharedPreferences.getString(LATITUDE,"");
    }

    public void setWXPayResult(int result){
        mEditor.putInt(WXPAY_RESULT,result);
        mEditor.apply();
    }
    public int getWXPayResult(){
        return mSharedPreferences.getInt(WXPAY_RESULT,110);
    }
}
